package com.pokermind.model.exception;

import com.pokermind.model.enums.ErrorCode;

/**
 * 手牌管理专用异常类 扑克业务中手牌生命周期管理的异常处理
 *
 * @author PokerMind Team
 */
public class HandManagementException extends PokerBusinessException {

    public HandManagementException(ErrorCode errorCode) {
        super(errorCode);
    }

    public HandManagementException(ErrorCode errorCode, String details) {
        super(errorCode, details);
    }

    public HandManagementException(ErrorCode errorCode, String details, Throwable cause) {
        super(errorCode, details, cause);
    }

    // === 手牌创建相关异常 ===

    /**
     * 手牌ID重复异常 当尝试创建已存在的手牌ID时抛出
     */
    public static class HandAlreadyExistsException extends HandManagementException {
        public HandAlreadyExistsException(String handId) {
            super(ErrorCode.HAND_ALREADY_EXISTS, String.format("Hand ID '%s' already exists in the system", handId));
        }
    }

    /**
     * 手牌未找到异常 当尝试操作不存在的手牌时抛出
     */
    public static class HandNotFoundException extends HandManagementException {
        public HandNotFoundException(Long handRecordId) {
            super(ErrorCode.HAND_NOT_FOUND, String.format("Hand with record ID %d not found", handRecordId));
        }

        public HandNotFoundException(String handId) {
            super(ErrorCode.HAND_NOT_FOUND, String.format("Hand with ID '%s' not found", handId));
        }
    }

    /**
     * 手牌状态无效异常 当手牌状态不允许当前操作时抛出
     */
    public static class HandInvalidStateException extends HandManagementException {
        public HandInvalidStateException(String currentState, String requiredState) {
            super(ErrorCode.HAND_INVALID_STATE,
                String.format("Hand is in state '%s' but requires '%s' for this operation", currentState,
                    requiredState));
        }
        
        // 直接消息构造函数 (用于直接传递详细错误消息)
        public HandInvalidStateException(String message) {
            super(ErrorCode.HAND_INVALID_STATE, message);
        }
    }

    /**
     * 手牌已完成异常 当尝试修改已结束的手牌时抛出
     */
    public static class HandAlreadyCompletedException extends HandManagementException {
        public HandAlreadyCompletedException(Long handRecordId) {
            super(ErrorCode.HAND_ALREADY_COMPLETED,
                String.format("Hand with record ID %d is already completed", handRecordId));
        }
    }

    // === 街道转换相关异常 ===

    /**
     * 街道转换无效异常 当街道转换顺序错误时抛出
     */
    public static class InvalidStreetTransitionException extends HandManagementException {
        public InvalidStreetTransitionException(String fromStreet, String toStreet) {
            super(ErrorCode.HAND_INVALID_STREET_TRANSITION,
                String.format("Cannot transition from '%s' to '%s'", fromStreet, toStreet));
        }
    }

    /**
     * 公共牌数量不匹配异常 当街道的公共牌数量不正确时抛出
     */
    public static class BoardCardsMismatchException extends HandManagementException {
        public BoardCardsMismatchException(String street, int expectedCards, int actualCards) {
            super(ErrorCode.HAND_BOARD_CARDS_MISMATCH,
                String.format("Street '%s' expects %d cards but got %d", street, expectedCards, actualCards));
        }
    }

    // === 玩家相关异常 ===

    /**
     * 座位已被占用异常
     */
    public static class SeatOccupiedException extends HandManagementException {
        public SeatOccupiedException(int seatNumber) {
            super(ErrorCode.PLAYER_SEAT_OCCUPIED, String.format("Seat number %d is already occupied", seatNumber));
        }
    }

    /**
     * 无Hero玩家异常
     */
    public static class NoHeroPlayerException extends HandManagementException {
        public NoHeroPlayerException() {
            super(ErrorCode.PLAYER_NO_HERO);
        }
    }

    /**
     * 用户未找到异常
     */
    public static class UserNotFoundException extends HandManagementException {
        public UserNotFoundException() {
            super(ErrorCode.USER_NOT_FOUND);
        }
    }

    /**
     * JSON解析异常
     */
    public static class JsonParseException extends HandManagementException {
        public JsonParseException() {
            super(ErrorCode.JSON_PARSE_ERROR);
        }
    }

    // === 数据验证相关异常 ===
    
    /**
     * 必填字段为空异常
     */
    public static class EmptyFieldException extends HandManagementException {
        public EmptyFieldException() {
            super(ErrorCode.VALIDATION_EMPTY_FIELD);
        }
    }
    
    /**
     * 字段长度超限异常
     */
    public static class FieldTooLongException extends HandManagementException {
        public FieldTooLongException() {
            super(ErrorCode.VALIDATION_FIELD_TOO_LONG);
        }
    }
    
    /**
     * 座位号无效异常
     */
    public static class InvalidSeatException extends HandManagementException {
        public InvalidSeatException() {
            super(ErrorCode.VALIDATION_INVALID_SEAT);
        }
    }
    
    /**
     * 字段值重复异常
     */
    public static class DuplicateFieldException extends HandManagementException {
        public DuplicateFieldException() {
            super(ErrorCode.VALIDATION_DUPLICATE_FIELD);
        }
    }
    
    /**
     * 无效卡牌格式异常
     */
    public static class InvalidCardsException extends HandManagementException {
        public InvalidCardsException() {
            super(ErrorCode.VALIDATION_INVALID_CARDS);
        }
    }
    
    /**
     * 玩家数量超限异常
     */
    public static class TooManyPlayersException extends HandManagementException {
        public TooManyPlayersException() {
            super(ErrorCode.VALIDATION_TOO_MANY_PLAYERS);
        }
    }

    /**
     * 多个Hero玩家异常
     */
    public static class MultipleHeroPlayersException extends HandManagementException {
        public MultipleHeroPlayersException() {
            super(ErrorCode.PLAYER_MULTIPLE_HERO);
        }
    }

    /**
     * 玩家数量不足异常
     */
    public static class InsufficientPlayersException extends HandManagementException {
        public InsufficientPlayersException(int playerCount) {
            super(ErrorCode.PLAYER_INSUFFICIENT_PLAYERS,
                String.format("Need at least 2 players, but got %d", playerCount));
        }
    }

    /**
     * 筹码数无效异常
     */
    public static class InvalidStackException extends HandManagementException {
        public InvalidStackException(String playerName, String stackAmount) {
            super(ErrorCode.PLAYER_INVALID_STACK,
                String.format("Player '%s' has invalid stack amount: %s", playerName, stackAmount));
        }
    }
}
